package com.diandiansong.app.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diandiansong.app.R;

/**
 * Created by chris on 2018/3/22.
 * 搜索组合控件
 */

public class SearchView extends LinearLayout {

    private EditText et_search;

    public SearchView(Context context) {
        super(context);
        init();
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.view_search, this);

        et_search = findViewById(R.id.et_search);
        final ImageView iv_delete = findViewById(R.id.iv_delete);
        iv_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search.setText("");
            }
        });
//        final TextView tv_cancel = findViewById(R.id.tv_cancel);
//        tv_cancel.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                iv_delete.setVisibility(View.GONE);
//                tv_cancel.setVisibility(View.GONE);
//                et_search.clearFocus();
//                KeyBoardUtils.closeKeybord(et_search, getContext());
//                inputListener.cancel();
//            }
//        });
        et_search.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    iv_delete.setVisibility(View.VISIBLE);
//                    tv_cancel.setVisibility(View.VISIBLE);
                } else {
                    iv_delete.setVisibility(View.GONE);
//                    tv_cancel.setVisibility(View.GONE);
                }
            }
        });

        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case EditorInfo.IME_ACTION_SEARCH:
                        inputListener.input(getInputInfo());
                        break;
                }
                return false;
            }
        });
    }

    //设置输入提示信息
    public void setHintInfo(String hintInfo) {
        et_search.setHint(hintInfo);
    }

    //获取输入内容
    public String getInputInfo() {
        return et_search.getText().toString().trim();
    }

    //获取输入内容
    public EditText getEditText() {
        return et_search;
    }

    public interface InputListener {
        void input(String input);

        void cancel();
    }

    private InputListener inputListener;

    public void setInputListener(InputListener inputListener) {
        this.inputListener = inputListener;
    }

    public void setCusOnClickListener(OnClickListener onClickListener) {
        et_search.setFocusable(false);
        et_search.setOnClickListener(onClickListener);
    }
}
